<!--
@author  tanyuze
@desc    打卡活动统计
@date    2022/11/10
-->
<template>
  <div id="hundredLearningSign">
    <section class="overall-data">活动总体数据</section>
    <div class="box-card">
      <el-row :gutter="20">
        <el-col>
          <div class="box-card-content">
            <span>报名总人数</span>
            <span class="din-front">{{ staticsData.registUserNum || '0' }}</span>
          </div>
        </el-col>
        <el-col>
          <div class="box-card-content">
            <span>打卡总人数</span>
            <span class="din-front">{{ staticsData.clockRecordNum || '0' }}</span>
          </div>
        </el-col>
        <el-col>
          <div class="box-card-content">
            <span>打卡总次数</span>
            <span class="din-front">{{ staticsData.clockRecordUserNum || '0' }}</span>
          </div>
        </el-col>
        <el-col>
          <div class="box-card-content">
            <span>{{ type==='CLOCK_ZYLX_CG'?'闯关总次数':'学习总时长' }}</span>
            <span class="din-front">{{ (staticsData.learnDuration && staticsData.learnDuration) || '0' }}{{ type==='CLOCK_ZYLX_CG'?'次':'分钟' }}</span>
          </div>
        </el-col>
        <el-col>
          <div class="box-card-content">
            <span>总分享次数</span>
            <span class="din-front">{{ staticsData.shareNum || '0' }}</span>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- <div class="activity-data">
      <el-row :gutter="20">
        <el-col>
          <div class="activity-data-content">
            <span>报名总人数：</span>
            <span class="din-front">{{ staticsData.joinUserNum || '0' }}</span>
          </div>
        </el-col>
        <el-col>
          <div class="activity-data-content">
            <span>打卡总人数：</span>
            <span class="din-front">{{ staticsData.clockUserNum || '0' }}</span>
          </div>
        </el-col>
        <el-col>
          <div class="activity-data-content">
            <span>打卡总次数：</span>
            <span class="din-front">{{ staticsData.clockAllNum || '0' }}</span>
          </div>
        </el-col>
      </el-row>
    </div> -->

    <el-collapse-transition>
      <div v-show="searchshow" class="common-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input v-model="listQuery.realName" clearable placeholder="按用户名进行搜索" class="search-input" @keyup.enter.native="handleFilter" />
        <div class="common-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">{{ $t('table.search') }}</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
        </div>
      </div>
    </el-collapse-transition>

    <div class="btn-container">
      <div class="btn-title">
        <span>打卡统计</span>
      </div>
      <div class="btn-btns">
        <el-button style="margin-left: 10px;" @click="handleExport">导出</el-button>
        <el-button style="margin-left: 10px;" @click="$router.go(-1)">返回</el-button>
      </div>
    </div>
    <div class="table">
      <v-table
        v-if="tag"
        :list="list"
        :tag="tag"
        :source-arr="sourceArr"
        :selection="true"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        @handleSelectionChange="handleSelectionChange"
      >
        <el-table-column label="操作" fixed="right" align="center" width="150" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleLook(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </v-table>
    </div>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { getStatistics, getStatisticsList, maskAPI, noMaskAPI } from '../api'
import { getToken } from '@/utils/auth'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import vTable from '@/components/Table'
import { exportSign } from '@/utils/fileImportOrExportSign'
import { exportCheck } from '@/utils/sensitiveInfoExportCheck'
export default {
  name: 'PunchClockSign',
  components: { Pagination, vTable },
  directives: { waves },
  // 过滤器
  filters: {
    ellipsis6lue(value) {
      if (!value) return ''
      if (value.length > 6) {
        return value.slice(0, 6) + '...'
      }
      return value
    }
  },
  data() {
    return {
      id: '',
      title: '',
      searchshow: true,
      listQuery: {
        page: 1,
        limit: 10,
        realName: ''
      },
      list: [],
      multipleSelection: [],
      total: 0,
      listLoading: false,
      sourceArr: [],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7
        }
      },
      staticsData: {},
      type: '',
      tag: ''
    }
  },
  created() {
    if (!this.$route.query.id) {
      this.$message({
        showClose: true,
        message: '参数异常！',
        type: 'warning',
        onClose: () => {
          this.$router.back()
        }
      })
      return
    } else {
      this.id = this.$route.query.id
      this.title = this.$route.query.title
      this.type = this.$route.query.type
      this.tag = this.$route.query.type === 'CLOCK_ZYLX_CG' ? 'clockStat-CG' : 'clockStat'
      this.getStatics()
      this.getList()
    }
  },
  methods: {
    filtercolumn(scope, item) {
      return scope.row[item]
      // if (item === 'learnDuration') {
      //   return scope.row[item] / 60 + '分钟'
      // } else {
      //   return scope.row[item]
      // }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleExport() {
      const query = exportSign({
        activityId: this.id,
        realName: this.listQuery.realName
      }).obj
      exportCheck('STAFF_CLOCK_ACTIVITY', this.id, maskAPI, noMaskAPI, query, '打卡活动统计信息.xls')
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resettingBtn() {
      this.listQuery.realName = ''
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 格式化时间 传入s
     */
    formatterTime(str) {
      var h = Math.floor(str / 3600000)
      var m = Math.floor((str % 3600000) / 60000)
      var s = Math.floor((str % 60000) / 1000)
      var ms = Math.floor(str % 1000)
      return this.toDou(h) + ':' + this.toDou(m) + ':' + this.toDou(s) + ':' + this.toTwoDou(ms)
    },
    toDou(n) {
      if (n >= 10) {
        return '' + n
      } else {
        return '0' + n
      }
    },
    toTwoDou(n) {
      if (n < 10) {
        return '00' + n
      } else if (n >= 100) {
        return '' + n
      } else {
        return '0' + n
      }
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        activityId: this.id,
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        realName: this.listQuery.realName
      }
      return new Promise((resolve, reject) => {
        getStatisticsList(query)
          .then((response) => {
            this.list = response.data.data.rows
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch((error) => {
            reject(error)
            this.listLoading = false
          })
      })
    },
    getStatics() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      var query = {
        id: this.id
      }
      return new Promise((resolve, reject) => {
        getStatistics(query)
          .then((response) => {
            loading.close()
            if (response.data.code === '200') {
              this.staticsData = response.data.data
            }
          })
          .catch((error) => {
            reject(error)
            loading.close()
          })
      })
    },
    rowClass({ row, rowIndex }) {
      return {
        padding: '5px 0',
        height: '20px',
        background: 'rgba(247,247,248,1)'
      }
    },
    handleLook(item) {
      this.$router.push({
        path: this.$filterPath('signinfo'),
        query: {
          id: item.activityId,
          title: this.title,
          userId: item.userId,
          realName: item.realName,
          type: this.type
        }
      })
    }
  }
}
</script>

<style type="text/scss" lang="scss">
$dark_gray: #bfced6;
$mainColor: #18a5fd;
#hundredLearningSign {
  padding: 20px;

  .overall-data {
    margin: 0 0 15px 0;
    font-size: 16px;
    font-weight: bold;
    font-family: PingFangSC-Medium;
    color: rgba(51, 51, 51, 1);
  }

  .box-card {
    margin-bottom: 10px;
    text-align: center;
    background-color: #fff;
    padding: 20px;
    .el-col {
      width: 20% !important;
      .box-card-content {
        display: flex;
        flex-direction: column;
        justify-content: center;
        font-size: 16px;
        color: #666;
        border-radius: 8px;
        padding: 14px 0;
        span:last-child {
          margin-top: 10px;
          font-size: 24px;
          font-weight: bold;
          line-height: 1;
        }
      }
      &:first-child .box-card-content {
        background: #fff2f1;
        span:last-child {
          color: #e44d42;
        }
      }
      &:nth-child(2) .box-card-content {
        background: #f3faff;
        span:last-child {
          color: #3eaaf9;
        }
      }
      &:nth-child(3) .box-card-content {
        background: #fff8f1;
        span:last-child {
          color: #f08628;
        }
      }
      &:nth-child(4) .box-card-content {
        background: #f4ffe7;
        span:last-child {
          color: #82bc3c;
        }
      }
      &:nth-child(5) .box-card-content {
        background: #f8f5ff;
        span:last-child {
          color: #966af9;
        }
      }
    }
    .current-position-name {
      font-size: 18px;
      font-weight: bold;
      margin-bottom: 10px;
    }
  }
  .activity-data {
    margin-bottom: 10px;
    text-align: center;
    background-color: #fff;
    padding: 0;

    .el-col {
      width: 33.3% !important;

      .activity-data-content {
        /*   display: flex;
        flex-direction: column;
        justify-content: center;*/
        font-size: 16px;
        color: #666;
        border-radius: 8px;
        padding: 5px 0;

        span:last-child {
          margin-top: 10px;
          font-size: 24px;
          font-weight: bold;
          line-height: 1;
        }
      }
    }
  }

  .common-search {
    background-color: #fff;
    padding: 0 20px;
    .svg-container {
      padding: 0;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .common-search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .common-search-more {
        font-size: 14px;
        font-weight: 500;
        color: $mainColor;
        cursor: pointer;
        margin-left: 10px;
      }
    }
    &.merge {
      height: 50px;
      line-height: 50px;
    }
    &.separate {
      .common-search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .common-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .btn-container {
    display: flex;
    height: 36px;
    line-height: 36px;
    margin-top: 20px;
    margin-bottom: 20px;
    .btn-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      font-family: PingFangSC-Medium;
      color: rgba(51, 51, 51, 1);
    }
  }
}
</style>
